//
//  BaseViewController.m
//  Student
//
//  Created by 马腾飞 on 15/9/8.
//  Copyright (c) 2015年 XueYinYue. All rights reserved.
//

#import "BaseViewController.h"
#import "MBProgressHUD.h"
#import "EACBizService.h"
#import "EACService.h"
#import "EACRemoteServiceException.h"

#import <MobClick.h>


@interface BaseViewController ()

- (void)handlerException:(NSException *)ex;

@end

@implementation BaseViewController {
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Alert view delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0 && _alertViewCancelBlock){
        [self performSelectorOnMainThread:@selector(performAlertViewCancelBlock) withObject:nil waitUntilDone:NO];
    }
    
    if(buttonIndex == 1 && _alertViewOkBlock){
        [self performSelectorOnMainThread:@selector(performAlertViewOKBlock) withObject:nil waitUntilDone:NO];
    }
}

#pragma mark - Text field delegete

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


#pragma mark - Event

- (void)backBarButtonItemOnClick:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Function

- (void)hideBackBarButtonItem {
    self.navigationItem.leftBarButtonItem = nil;
}


- (void)showToastMessage:(NSString *)message {
    MBProgressHUD *hud = nil;
    if (self.navigationController.view) {
        hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    } else {
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    
    
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.labelText = message;
    hud.margin = 10.f;
    hud.yOffset = -100;
    hud.removeFromSuperViewOnHide = YES;
    
    [hud hide:YES afterDelay:2];
    
}

- (void)showAlertMessage:(NSString *)message {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}

- (void)showAlertTitle:(NSString *)title withMessage:(NSString *)message {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}

- (void)showAlertMessage:(NSString *)message withOKHandler:(void (^)(void))okHandler andCancelHandler:(void (^)(void))cancelHandler {
    self.alertViewOkBlock = okHandler;
    self.alertViewCancelBlock = cancelHandler;
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}

- (void)performAlertViewOKBlock {
    _alertViewOkBlock();
}

- (void)performAlertViewCancelBlock {
    _alertViewCancelBlock();
}

- (void)showStatusBarMessage:(NSString *)message {
    MTStatusBarOverlay *overlay = [MTStatusBarOverlay sharedInstance];
    
    overlay.animation = MTStatusBarOverlayAnimationFallDown;  // MTStatusBarOverlayAnimationShrink
    overlay.delegate = self;
    
    [overlay postImmediateFinishMessage:message duration:3.0 animated:YES];
    
}


- (void)showProgressView {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hud setLabelText:@"处理中..."];
    hud.yOffset = -100;
}



- (void)showProgressViewWithMessage:(NSString *)message{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hud setLabelText:message];
    hud.yOffset = -100;
}

- (void)showProgressViewInWindowDelayWithMessage:(NSString *)message{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].delegate.window.rootViewController.view animated:YES];
    [hud setLabelText:message];
    hud.yOffset = -100;
    [hud hide:YES afterDelay:1];
}

- (void)showProgressViewInWindowWithMessage:(NSString *)message{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].delegate.window.rootViewController.view animated:YES];
    [hud setLabelText:message];
    hud.yOffset = -100;
}

#pragma mark - 只有文字的HUD
- (void)showProgressViewWithPlainTextInWindowWithMessage:(NSString *)message {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].delegate.window.rootViewController.view animated:YES];
    [hud setLabelText:message];
    hud.mode = MBProgressHUDModeText;
    [hud hide:YES afterDelay:1];
    hud.yOffset = -100;
}

- (void)hideProgressView {
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}

- (void)hideProgressViewInWindow{
    [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].delegate.window.rootViewController.view animated:YES];
    
}

- (void)processNotification:(NSNotification *)notification {
    
}

- (void)addObserverNotification:(NSString *)name {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processNotification:) name:name object:nil];
}

#pragma mark - Run Service


- (void)runService:(EACService *)service andComplete:(void (^)(EACService *))onComplete andSuccess:(void (^)(EACService *, id))onSuccess andFault:(void (^)(EACService *, NSException *))onFault {
    [service asyncExecuteWithComplete:onComplete andSuccess:onSuccess andFault:^(EACService *service, NSException *exception) {
        [self handlerException:exception];
        
        if(onFault){
            onFault(service, exception);
        }
    }];
}

- (void)runService:(EACService *)service andComplete:(void (^)(EACService *))onComplete andSuccess:(void (^)(EACService *, id))onSuccess {
    [self runService:service andComplete:onComplete andSuccess:onSuccess andFault:nil];
}

- (void)runService:(EACService *)service andComplete:(void (^)(EACService *))onComplete {
    [self runService:service andComplete:onComplete andSuccess:nil andFault:nil];
}

- (void)runService:(EACService *)service {
    [self runService:service andComplete:nil andSuccess:nil andFault:nil];
}

#pragma mark - Handler Exception

- (void)handlerException:(NSException *)ex {
    if ([ex isKindOfClass:[EACRemoteServiceException class]]) {
        [self showToastMessage:[NSString stringWithFormat:@"无法连接服务器，请稍候重试"]];
    } else {
        [self showToastMessage:[NSString stringWithFormat:@"出现异常 %@", ex.reason]];
    }
}


#pragma mark - 点击屏幕结束编辑
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - 友盟页面统计
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"pageOne"];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"pageOne"];
}
@end
